//
//  MenuViewController.m
//  iTestPro
//
//  Created by Deepak Kumar on 2/3/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "MenuViewController.h"
#import "MenuTableCell.h"
#import "RevealController.h"
//#import "Country.h"
//#import "Stations.h"
//#import "Apps.h"
#import "ImageViewController.h"
#import "UserSettingViewController.h"

@implementation MenuViewController
{
    NSIndexPath *mSelectedIndex;
    NSInteger mStartIndex, mItemsPerPage;
}

@synthesize mTableView;
//@synthesize mObjFMPlayerViewController;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    mSelectedIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    
    if (IS_IPHONE_5) {
       // self.mTableView.frame = CGRectMake(0, 44, 320, 525);
    }
    
    //[self.mTableView setSeparatorColor:[UIColor colorWithRed:(33.0/255) green:(33.0/255) blue:(33.0/255) alpha:1.0]];
    

    [self.mTableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG_GrayBase"]]];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
   // mObjCouponsViewController = [[CouponsViewController alloc] initWithNibName:@"CouponsViewController" bundle:nil];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void) refeashMenuList
{
    NSLog(@"------refeashMenuList");
    
    [self.mTableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"BG_MenuiPhone5_%d", arc4random()%11]]]];

    
//    if (mNewAppsArray) {
//        [mNewAppsArray removeAllObjects];
//        mNewAppsArray = nil;
//    }
    
    //mNewAppsArray = [[NSMutableArray alloc] initWithArray:[[Repository sharedRepository] fetchAllNewApps]];
    
    [self.mTableView reloadData];
    
    
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    switch (section) {
		case 0:
			return 1;
        case 1: // Folders
			return 4;
		default:
			return 0;
	}
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return section == 0 ? 0.0 : 26.0;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 26)];
    
    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DividerBar"]];
    
    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 26);
    [headerView addSubview:headerImage];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.bounds.size.width - 10, 18)];
    //label.font = [UIFont systemFontOfSize:14.0];
   // label.font = [UIFont boldSystemFontOfSize:14.0];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
    switch (section) {
		case 0:
            label.text = @"";
                break;
        case 1:
            label.text = @"Folder";
                break;
		default:
            label.text = @"";
                break;
	}
    
    label.textColor = [UIColor blackColor]; //[UIColor colorWithRed:(252.0/255.0) green:(67.0/255.0) blue:(52.0/255.0) alpha:1.0];
    //label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    
    [headerView addSubview:label];
    
    
    [headerView setBackgroundColor:[UIColor clearColor]];
    return headerView;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"MenuTableCell";
    MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	if (cell == nil) {
		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MenuTableCell" owner:self options:nil];
		cell =  (MenuTableCell *)[topLevelObjects objectAtIndex:0];
	}
    
    cell.mSelectedImageView.image = nil;
    //UIFont *myFont = [ UIFont fontWithName: @"Helvetica Neue Bold" size: 19.0 ];
    //cell.mTitleLabel.textColor = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1.0];
    //cell.mTitleLabel.font = myFont;

    
    switch (indexPath.section) {
        case 0:
        {
            cell.mTitleLabel.text = @"User Name";
            //cell.mTitleLabel.font = myFont;
            cell.mImageView.image = [UIImage imageNamed:@"Icon"];
            
        }
            break;
        
        case 1:
        {
//            Stations *tStation = [mPopularStationArray objectAtIndex:indexPath.row];
//            cell.mTitleLabel.text = tStation.mTitle;
//            NSLog(@"FileName:%@", tStation.mIconImg);
//                        
//            if (isFileExists(tStation.mIconImg)) {
//                cell.mImageView.image =  [UIImage imageWithContentsOfFile:imageFilePath(tStation.mIconImg)];
//            }
//            else{
//                
//                cell.mImageView.image = [UIImage imageNamed:@"BG_Placeholder"];
//                if ([tStation.mIconImg length] > 0) {
//                    [[IconDownloadManager getInstance] setScreen:kMenuScreen delegate:self
//                                        filePath:[kImageURL stringByAppendingString:tStation.mIconImg]
//                                iconID:[NSString stringWithFormat:@"mIcnId%d",arc4random()%1000] indexPath:indexPath];
//                }
//            }            
        }
            break;
              default:
            break;
    }
    
    // Make cell selected.
    if (indexPath.section == mSelectedIndex.section && indexPath.row == mSelectedIndex.row) {
        NSLog(@"mSelectedIndex>>>>");
        cell.mSelectedImageView.image = [UIImage imageNamed:@"side_menu_selected_cell"];
    }

    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    RevealController *revealController = [self.parentViewController isKindOfClass:[RevealController class]] ? (RevealController *)self.parentViewController : nil;
    
    switch (indexPath.section) {
        case 0:
        {            
            if ([revealController.frontViewController isKindOfClass:[UINavigationController class]] && ![((UINavigationController *)revealController.frontViewController).topViewController isKindOfClass:[UserSettingViewController class]])
            {
                UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:[[UserSettingViewController alloc]initWithNibName:@"UserSettingViewController" bundle:nil]];
                [revealController setFrontViewController:navigationController animated:NO];
            }
            else
            {
                [revealController revealToggle:self];
            }
        }
            break;
        case 1:
        {
            if ([revealController.frontViewController isKindOfClass:[UINavigationController class]] && ![((UINavigationController *)revealController.frontViewController).topViewController isKindOfClass:[ImageViewController class]])
            {
                UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:[[ImageViewController alloc]initWithNibName:@"ImageViewController" bundle:nil]];
                [revealController setFrontViewController:navigationController animated:NO];
            }
            else
            {
                [revealController revealToggle:self];
            }
        }
            break;
       
        default:
            break;
    }
    
 

    mSelectedIndex = indexPath;
    [self.mTableView reloadData];
}




#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


@end

